package com.fancy.mobileschool.ui;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.fancy.mobileschool.MyConfiguration;
import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.Friend;
import com.fancy.mobileschool.service.ChatFriendsService;
import com.fancy.mobileschool.task.TaskExecutor;



public class AddFriendActivity extends AppCompatActivity implements TextView.OnEditorActionListener , View.OnClickListener{

    private EditText search;

    private ChatFriendsService chatFriendsService;

    private TextView friendName;

    private TextView motto;

    private View friendItemHolder;

    private Friend friend;

    private Button button;

    private final static int showSearchFriendResult = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        chatFriendsService = new ChatFriendsService();
        setContentView(R.layout.activity_add_friend);
        search = (EditText) findViewById(R.id.search);
        friendName = (TextView) findViewById(R.id.chat_friends_item_nickname);
        motto = (TextView) findViewById(R.id.chat_friends_item_motto);
        button = (Button) findViewById(R.id.add_friend_button);
        button.setOnClickListener(this);
        friendItemHolder = findViewById(R.id.friend_item_holder);
        search.setOnEditorActionListener(this);
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        String friendID = textView.getText().toString();
        boolean isValid = checkText(friendID);
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && isValid){
            searchFriend(friendID);
        }

        return true;
    }

    private boolean checkText(String text) {
        if (text == null || text.equals(""))
            return false;

        int size = text.length();
        for (int i = 0 ; i < size ; i++) {
            char c = text.charAt(i);
            if (c == ' ')
                return false;
        }
        return true;
    }

    private void searchFriend(final String friendID) {
        TaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                friend = chatFriendsService.findFriend(friendID);
                handler.sendEmptyMessage(showSearchFriendResult);
            }
        });
    }

    private void addFriend(final String friendID) {
        TaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                chatFriendsService.addFriend(MyConfiguration.myID, friendID);
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == showSearchFriendResult) {
                if (friend != null) {
                    friendName.setText(friend.name);
                    motto.setText(friend.motto);
                    friendItemHolder.setVisibility(View.VISIBLE);
                }else {
                    friendItemHolder.setVisibility(View.INVISIBLE);
                    Toast.makeText(AddFriendActivity.this, "没有此用户", Toast.LENGTH_SHORT).show();
                }
            }

        }
    };

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.add_friend_button && friend != null) {
            addFriend(friend.id);
            Toast.makeText(AddFriendActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
        }
    }
}
